package com.example.demos.service.impl;

import com.example.demos.dto.SupplierDTO;
import com.example.demos.entity.Supplier;
import com.example.demos.mapper.SupplierMapper;
import com.example.demos.service.SupplierService;
import com.example.demos.util.PageBean;
import com.example.demos.util.Validator;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierMapper supplierMapper;
    @Override
    public PageBean<List<Supplier>> selectSupplierListByPage(SupplierDTO supplierDTO) {
        Page<Supplier> page1 = PageHelper.startPage(supplierDTO.getPage(),supplierDTO.getRows());
        supplierMapper.selectSupplierListByPage(supplierDTO);
        return new PageBean<>(page1.getTotal(), page1.getResult());
    }
    @Override
    public boolean addSupplier(Supplier supplier) {
        if (Validator.isValidTele(supplier.getTelephone())&&Validator.isValidEmail(supplier.getEmail())){
          supplierMapper.addSupplier(supplier);
            return true;
        }
        return false;
    }
    @Override
    public void deleteSupplier(BigInteger id) {
        supplierMapper.deleteSupplier(id);
    }

    @Override
    public boolean selectExist(Supplier supplier) {
        if (supplierMapper.selectExist(supplier)!=null) {
            return false;
        };
        return true;
    }

    @Override
    public Supplier selectSupplierById(BigInteger id) {
        return supplierMapper.selectSupplier(id);
    }

    @Override
    public boolean updateSupplier(Supplier supplier) {
        if(Validator.isValidTele(supplier.getTelephone())&&Validator.isValidEmail(supplier.getEmail())){
            supplierMapper.updateSupplier(supplier);
            return true;
        }
        return false;
    }
}
